<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class Collage extends Model
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'collage';

    protected $guarded = [];

    public function intermediaryUser()
    {
        return $this->belongsTo(IntermediaryUser::class,'intermediary_id');
    }

    public function collageMember()
    {
        return $this->belongsToMany(WxUser::class,'collage_member','collage_id','user_id');
    }

    public function getStartTimeAttribute($value)
    {
        return date('Y.m.d',strtotime($value));
    }

    public function getEndTimeAttribute($value)
    {
        return date('Y.m.d',strtotime($value));
    }

    public static function getCollageData($intermediary_id)
    {
        return self::with('intermediaryUser')->where('intermediary_id',$intermediary_id)->get();
    }

    public static function getDetails($collage_id)
    {
        return self::where(['id'=>$collage_id])->first();
    }

    public static function updateData($collage_id)
    {
        return self::where('id',$collage_id)->update(['status'=>1]);
    }

    public static function changeStatus($collage_id)
    {
        $detail = self::getDetails($collage_id);
        if ($detail->status == 1){
            return self::where('id',$collage_id)->update(['status'=>0]);
        }else{
            return self::where('id',$collage_id)->update(['status'=>1]);
        }
    }

    public static function createData($data)
    {
        return self::create($data,true);
    }

    public static function getCollageInfo($collage)
    {
        return self::with('collageMember')->where('id',$collage)->first();
    }

    public static function minusPeopleNum($collage_id)
    {
        $collageInfo = self::find($collage_id);
        $people_num = $collageInfo->people_num - 1;
        return self::where('id',$collage_id)->update(['people_num'=>$people_num]);
    }

    public static function collageInfo($where)
    {
        return self::where($where)->first();
    }

    public static function deleteCollage($collage_id)
    {
        return self::where('id',$collage_id)->forceDelete();
    }
}
